package com.syty.demo.controller;

import com.syty.demo.Listener.LoginSessionListener;
import com.syty.demo.domain.User;
import com.syty.demo.result.ResultSet;
import com.syty.demo.service.UserService;
import com.syty.demo.util.LogUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
public class IndexController {

    @Autowired
    private UserService userService;

    /**
     * index.html
     * @param session
     * @return
     */
    @ApiOperation("跳转首页面")
    @GetMapping("/index")
    public ModelAndView indexMain(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        //判断是否为登录用户
        ResultSet user =(ResultSet) session.getAttribute("user");
        if (user==null){
            //用户未登录
            modelAndView.setViewName("loginAndregister");
        }

        LogUtils.write("[跳转首页]--获取loginSession : "+ user);
        modelAndView.setViewName("index");
        User userInfo = (User) user.getData();
        modelAndView.addObject("userInfo",userInfo);
        return modelAndView;
    }

    /**
     * error.html
     * @return
     */
    @ApiOperation("跳转到未授权页面")
    @RequestMapping("/unauthorized")
    public String unauthorized(){
        return "error";
    }

    /**
     * error.html
     * @return
     */
    @ApiOperation("跳转到异常页面")
    @RequestMapping("/toError")
    public String toError(){
        return "error";
    }


    /**
     * about-us.html
     * @return
     */
    @ApiOperation("跳转到关于我页面")
    @RequestMapping("/toAbout")
    public ModelAndView toAbout(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        ResultSet user = (ResultSet) session.getAttribute("user");
        if (user==null){
            //用户未登录
            modelAndView.setViewName("loginAndregister");
        }

        LogUtils.write("[跳转关于我页面]--获取loginSession : "+ user);
        modelAndView.setViewName("about-us");
        User userInfo = (User) user.getData();
        modelAndView.addObject("userInfo",userInfo);
        return modelAndView;
    }


    /**
     * info.html
     * 需要提供用户信息，供thymeleaf抓取信息到页面
     * @return
     */
    @ApiOperation("跳转到个人信息页面")
    @RequestMapping("/toInfo")
    public ModelAndView toInfo(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        ResultSet user = (ResultSet) session.getAttribute("user");
        if (user==null){
            //用户未登录
            modelAndView.setViewName("loginAndregister");
        }

        LogUtils.write("[跳转个人信息页面]--获取loginSession : "+ user);
        modelAndView.setViewName("info");
        User userInfo = (User) user.getData();
        modelAndView.addObject("userInfo",userInfo);

        return modelAndView;
    }


    /**
     * detail.html
     * 需要提供用户信息，供thymeleaf抓取信息到页面
     * @return
     */
    @ApiOperation("跳转到行业分析详情页面")
    @RequestMapping("/toDetail")
    public ModelAndView toDetail(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        ResultSet user = (ResultSet) session.getAttribute("user");
        if (user==null){
            //用户未登录
            modelAndView.setViewName("loginAndregister");
        }
        LogUtils.write("[跳转行业分析详情页面]--获取loginSession : "+ user);
        modelAndView.setViewName("detail");
        User userInfo = (User) user.getData();
        modelAndView.addObject("userInfo",userInfo);

        return modelAndView;
    }


    /**
     * table.html
     * 需要提供用户信息，供thymeleaf抓取信息到页面
     * @return
     */
    @ApiOperation("跳转到表分析页面")
    @RequestMapping("/toTable")
    public ModelAndView toTable(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        ResultSet user = (ResultSet) session.getAttribute("user");
        if (user==null){
            //用户未登录
            modelAndView.setViewName("loginAndregister");
        }
        LogUtils.write("[跳转表分析页面]--获取loginSession : "+ user);
        modelAndView.setViewName("table");
        User userInfo = (User) user.getData();
        modelAndView.addObject("userInfo",userInfo);

        return modelAndView;
    }


    /**
     * chart.html
     * 需要提供用户信息，供thymeleaf抓取信息到页面
     * @return
     */
    @ApiOperation("跳转到图分析页面")
    @RequestMapping("/toChart")
    public ModelAndView toChart(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        ResultSet user = (ResultSet) session.getAttribute("user");
        if (user==null){
            //用户未登录
            modelAndView.setViewName("loginAndregister");
        }
        LogUtils.write("[跳转图分析页面]--获取loginSession : "+ user);
        modelAndView.setViewName("chart");
        User userInfo = (User) user.getData();
        modelAndView.addObject("userInfo",userInfo);
        return modelAndView;
    }



    /**
     * DataShow.html
     * 需要提供用户信息，供thymeleaf抓取信息到页面
     * @return
     */
    @ApiOperation("跳转到一图览分析页面")
    @RequestMapping("/toDataShow")
    public ModelAndView toDataShow(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        ResultSet user = (ResultSet) session.getAttribute("user");
        if (user==null){
            //用户未登录
            modelAndView.setViewName("loginAndregister");
        }
        LogUtils.write("[跳转一图览分析页面]--获取loginSession : "+ user);
        modelAndView.setViewName("DataShow");
        User userInfo = (User) user.getData();
        modelAndView.addObject("userInfo",userInfo);
        return modelAndView;
    }

    /**
     * 当用户直接关闭页面时，尝试注销掉用户session
     * 不规范的推出，如果无法成功注销，下次登录会失败
     * @param session
     */
    @ApiOperation("当用户直接关闭页面时，尝试注销掉用户session")
    @RequestMapping("/logoutT")
    public void logoutT(HttpSession session){
        // 移除session信息
        LoginSessionListener.removeSession(session.getId());

    }

}
